/*
               File: PrcValidarCPF
        Description: Prc Validar CPF
             Author: GeneXus C# Generator version 10_2_5-78484
       Generated on: 6/6/2014 9:52:41.78
       Program type: Callable routine
          Main DBMS: sqlserver
*/
using System;
using System.Collections;
using GeneXus.Utils;
using GeneXus.Resources;
using GeneXus.Application;
using GeneXus.Metadata;
using GeneXus.Data;
using com.genexus;
using GeneXus.Data.ADO;
using GeneXus.Data.NTier;
using GeneXus.Data.NTier.ADO;
using System.Runtime.Remoting;
using GeneXus.WebControls;
using GeneXus.Http;
using GeneXus.Procedure;
using GeneXus.XML;
using GeneXus.Search;
using GeneXus.Encryption;
using GeneXus.Http.Client;
using System.Threading;
using System.Xml.Serialization;
using System.Data.SqlTypes;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
namespace GeneXus.Programs {
   public class prcvalidarcpf : GXProcedure
   {
      public prcvalidarcpf( )
      {
         context = new GxContext(  );
         DataStoreUtil.LoadDataStores( context);
         IsMain = true;
         context.SetDefaultTheme("GeneXusXEv2");
      }

      public prcvalidarcpf( IGxContext context )
      {
         this.context = context;
         IsMain = false;
      }

      public void release( )
      {
      }

      public void execute( ref long aP0_CPF ,
                           ref bool aP1_isValid )
      {
         this.AV8CPF = aP0_CPF;
         this.AV9isValid = aP1_isValid;
         initialize();
         executePrivate();
         aP0_CPF=this.AV8CPF;
         aP1_isValid=this.AV9isValid;
      }

      public bool executeUdp( ref long aP0_CPF )
      {
         this.AV8CPF = aP0_CPF;
         this.AV9isValid = aP1_isValid;
         initialize();
         executePrivate();
         aP0_CPF=this.AV8CPF;
         aP1_isValid=this.AV9isValid;
         return AV9isValid ;
      }

      public void executeSubmit( ref long aP0_CPF ,
                                 ref bool aP1_isValid )
      {
         prcvalidarcpf objprcvalidarcpf;
         objprcvalidarcpf = new prcvalidarcpf();
         objprcvalidarcpf.AV8CPF = aP0_CPF;
         objprcvalidarcpf.AV9isValid = aP1_isValid;
         objprcvalidarcpf.context.SetSubmitInitialConfig(context);
         objprcvalidarcpf.initialize();
         ThreadPool.QueueUserWorkItem( new WaitCallback( executePrivateCatch ),objprcvalidarcpf);
         aP0_CPF=this.AV8CPF;
         aP1_isValid=this.AV9isValid;
      }

      void executePrivateCatch( object stateInfo )
      {
         try
         {
            ((prcvalidarcpf)stateInfo).executePrivate();
         }
         catch ( Exception e )
         {
            GXUtil.SaveToEventLog( "Design", e);
            throw e ;
         }
      }

      void executePrivate( )
      {
         /* GeneXus formulas */
         /* Output device settings */
         if ( ! ((AV8CPF==11111111111L)||(AV8CPF==22222222222L)||(AV8CPF==33333333333L)||(AV8CPF==44444444444L)||(AV8CPF==55555555555L)||(AV8CPF==66666666666L)||(AV8CPF==77777777777L)||(AV8CPF==88888888888L)||(AV8CPF==99999999999L)||(AV8CPF==0)) )
         {
            AV11CPFChar = StringUtil.Str( (decimal)(AV8CPF), 11, 0);
            AV10Soma = (short)((10*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 1, 1), "."))+(9*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 2, 1), ".")));
            AV10Soma = (short)(AV10Soma+((8*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 3, 1), "."))+(7*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 4, 1), "."))));
            AV10Soma = (short)(AV10Soma+((6*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 5, 1), "."))+(5*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 6, 1), "."))));
            AV10Soma = (short)(AV10Soma+((4*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 7, 1), "."))+(3*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 8, 1), "."))));
            AV10Soma = (short)(AV10Soma+((2*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 9, 1), "."))));
            AV12CPFVerificador = StringUtil.Substring( AV11CPFChar, 1, 9);
            AV13Quociente = (short)(AV10Soma/ (decimal)(11));
            AV14Resto = (short)((int)(AV10Soma % (11)));
            if ( AV14Resto < 2 )
            {
               AV12CPFVerificador = AV12CPFVerificador + "0";
            }
            else
            {
               AV15Diferenca = (short)(11-AV14Resto);
               AV12CPFVerificador = AV12CPFVerificador + StringUtil.Trim( StringUtil.Str( (decimal)(AV15Diferenca), 4, 0));
            }
            AV10Soma = (short)((11*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 1, 1), "."))+(10*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 2, 1), ".")));
            AV10Soma = (short)(AV10Soma+((9*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 3, 1), "."))+(8*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 4, 1), "."))));
            AV10Soma = (short)(AV10Soma+((7*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 5, 1), "."))+(6*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 6, 1), "."))));
            AV10Soma = (short)(AV10Soma+((5*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 7, 1), "."))+(4*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 8, 1), "."))));
            AV10Soma = (short)(AV10Soma+((3*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 9, 1), "."))+(2*NumberUtil.Val( StringUtil.Substring( AV11CPFChar, 10, 1), "."))));
            AV13Quociente = (short)(AV10Soma/ (decimal)(11));
            AV14Resto = (short)((int)(AV10Soma % (11)));
            if ( AV14Resto < 2 )
            {
               AV12CPFVerificador = AV12CPFVerificador + "0";
            }
            else
            {
               AV15Diferenca = (short)(11-AV14Resto);
               AV12CPFVerificador = AV12CPFVerificador + StringUtil.Trim( StringUtil.Str( (decimal)(AV15Diferenca), 4, 0));
            }
            if ( StringUtil.StrCmp(AV11CPFChar, AV12CPFVerificador) == 0 )
            {
               AV9isValid = true;
            }
            else
            {
               AV9isValid = false;
            }
         }
         else
         {
            AV9isValid = false;
         }
         this.cleanup();
      }

      public void cleanup( )
      {
         CloseOpenCursors();
         if ( IsMain )
         {
            context.CloseConnections() ;
         }
         exitApplication();
      }

      protected void CloseOpenCursors( )
      {
      }

      public override void initialize( )
      {
         AV11CPFChar = "";
         AV12CPFVerificador = "";
         /* GeneXus formulas. */
         context.Gx_err = 0;
      }

      private short AV10Soma ;
      private short AV13Quociente ;
      private short AV14Resto ;
      private short AV15Diferenca ;
      private long AV8CPF ;
      private bool AV9isValid ;
      private String AV11CPFChar ;
      private String AV12CPFVerificador ;
      private long aP0_CPF ;
      private bool aP1_isValid ;
   }

   [ServiceContract(Namespace = "GeneXus.Programs.prcvalidarcpf_services")]
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
   public class prcvalidarcpf_services : GxRestService
   {
      [OperationContract]
      [WebInvoke(Method =  "POST" ,
      	BodyStyle =  WebMessageBodyStyle.Wrapped  ,
      	ResponseFormat = WebMessageFormat.Json,
      	UriTemplate = "/")]
      public void execute( ref String CPF ,
                           ref bool isValid )
      {
         try
         {
            if ( ! ProcessHeaders("prcvalidarcpf") )
            {
            }
            prcvalidarcpf worker = new prcvalidarcpf(context) ;
            worker.IsMain = RunAsMain ;
            long gxrCPF ;
            gxrCPF = (long)(NumberUtil.Val( (String)(CPF), "."));
            worker.execute(ref gxrCPF,ref isValid );
            worker.cleanup( );
            CPF = StringUtil.LTrim( StringUtil.Str( (decimal)(gxrCPF), 11, 0)) ;
         }
         catch ( Exception e )
         {
            WebException(e);
         }
         finally
         {
            Cleanup();
         }
      }

   }

}
